<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;


class Settings extends Common
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        error_reporting(0);
        ini_set("display_errors", "On");
    }

    /**
     * User:Simony
     * Function: 自动升级参数设置
     * @return mixed
     */
    public function upgrade()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }

        //model
        $levelModel = model('common/member/Level');
        $sexs = model('common/member/member')->sexs;

        //getData
        $level_list = $levelModel->getList([], 1000, 'id,name', 'level asc');
        $sexs = model('common/member/member')->sexs;


        $this->assign('level_list', $level_list);
        $this->assign('sexs', $sexs);
        $this->assign('sex_num', count($sexs));
        $this->assign('upgrade_condition', moduleConfig('upgrade_condition'));
        return $this->fetch();

    }

    /**
     * User:Simony
     * Function:会员邀请权限设置
     * @return mixed
     */
    public function inviteAuth()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }

        //model
        $levelModel = model('common/member/Level');
        //getData
        $level_list = $levelModel->getList([], 1000, 'id,name', 'level asc');
        $this->assign('level_list', $level_list);
        $this->assign('sexs', model('common/member/member')->sexs);
        $this->assign('invite_auth', moduleConfig('invite_auth')); //权限
        return $this->fetch();
    }

    /**
     * User:Simony
     * Function:佣金奖
     * @return mixed
     */
    public function commissionBonus()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }

        //model
        $levelModel = model('common/member/Level');
        //getData
        $level_list = $levelModel->getList([], 1000, 'id,name', 'level asc');
        $this->assign('level_list', $level_list);

        $sexs = model('common/member/member')->sexs;
        $this->assign('sexs', $sexs);
        $this->assign('sex_num', count($sexs));
        $this->assign('commission', moduleConfig('commission')); //推荐奖

        return $this->fetch();
    }

    /**
     * User:Simony
     * Function:推广奖
     * @return mixed
     */
    public function extensionBonus()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }

        //model
        $levelModel = model('common/member/Level');
        //getData
        $level_list = $levelModel->getList([], 1000, 'id,name', 'level asc');
        $this->assign('level_list', $level_list);

        $sexs = model('common/member/member')->sexs;
        $this->assign('sexs', $sexs);
        $this->assign('sex_num', count($sexs));
        $this->assign('extension', moduleConfig('extension')); //推荐奖

        return $this->fetch();
    }

    /**
     * User: Simony
     * Function:合伙人奖
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function partnerBonus()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }
        $identitys = model('common/member/member')->identitys;
        unset($identitys[0]);
        $this->assign('identitys', $identitys);
        $this->assign('partner', moduleConfig('partner'));

        return $this->fetch();
    }


    /**
     * User:Simony
     * Function:查看会员信息配置设置
     * @return mixed
     */
    public function cost()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }
        $this->assign('cost', moduleConfig('cost')); //推荐奖

        return $this->fetch();
    }


    public function contentPostAuth()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }

        //model
        $contentTypeModel = model('common/content/ContentType');


        //getData
        $contentTypeList = $contentTypeModel->getCol('id,name', []);
        $sexs = model('common/member/member')->sexs;

        $this->assign('sexs', $sexs);
        $this->assign('sex_num', count($sexs));
        $this->assign('content_type_list', $contentTypeList);
        $this->assign('content_post_auth', moduleConfig('content_post_auth')); //套餐权限

        return $this->fetch();
    }


    public function defaultSysNoticeConfig()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }
        //getData
        $this->assign('sexs', model('common/member/member')->sexs);
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function:赠送配置
     * @return mixed
     */
    public function givePackage()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }
        $this->assign('give_package', moduleConfig('give_package')); //赠送配置
        $this->assign('types', model('common/member/member')->types);
        $this->assign('package_list', model('common/package/Package')->getList([], 0, '*', 'sort desc'));
        $this->assign('sexs', model('common/member/member')->sexs);
        return $this->fetch();
    }


    //=======================================================================================================//

    /**
     * User:Simony
     * Function: 公共设置方法
     */
    public function setSetting()
    {

        $data = $this->request->post();
        foreach ($data as $key => $value) {
            moduleConfig($key, $value);
        }
        return ds_arr('ret', '保存成功');
    }


    /**
     * User:Simony
     * Function:等级权限
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function levelAuth()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }

        //model
        $levelModel = model('common/member/Level');
        $sexs = model('common/member/member')->sexs;

        //getData
        $level_list = $levelModel->getList([], 1000, 'level,name', 'level asc');

        $this->assign('levelModel', $levelModel);
        $this->assign('sexs', $sexs);
        $this->assign('sex_num', count($sexs));
        $this->assign('level_list', $level_list);
        $this->assign('level_auth', moduleConfig('level_auth')); //套餐权限

        return $this->fetch();
    }

    /**
     * User:Simony
     * Function: 等级使用权限
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function levelUseAuth()
    {
        if ($this->request->isAjax()) {
            $res = $this->setSetting();
            ds_json_encode($res['code'], $res['msg']);
        }

        //model
        $levelModel = model('common/member/Level');
        $sexs = model('common/member/member')->sexs;

        //getData
        $level_list = $levelModel->getList([], 1000, 'level,name', 'level asc');

        $this->assign('levelModel', $levelModel);
        $this->assign('sexs', $sexs);
        $this->assign('sex_num', count($sexs));
        $this->assign('level_list', $level_list);
        $this->assign('level_use_auth', moduleConfig('level_use_auth')); //套餐权限

        return $this->fetch();
    }
}